package com.ljz.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ljz.api.bean.SpuBean;
import com.ljz.common.dao.MyBaseServiceImpl;

import com.ljz.common.dao.bean.PageResult;
import com.ljz.entity.*;
import com.ljz.mapper.*;
import com.ljz.query.SpuQuery;
import com.ljz.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.ljz.common.utils.Assert.isSuccess;

import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service
public class SpuServiceImpl extends MyBaseServiceImpl<Spu, SpuMapper> implements SpuService {

    @Autowired
    SkuMapper skuMapper;

    @Autowired
    SpuAttrValueMapper spuAttrValueMapper;

    @Autowired
    AttrMapper attrMapper;

    @Autowired
    SpuDetailMapper spuDetailMapper;

    @Autowired
    AttrValueMapper attrValueMapper;

    @Autowired
    SkuStockMapper skuStockMapper;

    @Autowired
    SpuExtensionMapper spuExtensionMapper;

    @Autowired
    SpuSkuAttrValueMapper spuSkuAttrValueMapper;

    @Autowired
    SkuStockLockMapper skuStockLockMapper;

    @Override
    @Transactional
    public int saveEntity(Spu entity) {

        //保存spu
        int re = baseMapper.insert(entity);
        isSuccess(re, "插入spu失败");

        //保存sku
        int total = 0;
        if (null != entity.getSkuList() && entity.getSkuList().size() > 0) {
            List<Sku> list = entity.getSkuList();
            for (Sku sku : list) {
                sku.setSpu_id(entity.getSpu_id());
                re = skuMapper.insert(sku);
                isSuccess(re, "插入sku失败");

                //保存库存
                total+= sku.getActual_stock();//记录总库存

                SkuStock skuStock = new SkuStock();
                skuStock.setSku_id(sku.getSku_id());
                skuStock.setActual_stock(sku.getActual_stock());
                skuStock.setStock(sku.getActual_stock());

                skuStockMapper.insert(skuStock);
            }
        }

        //保存属性
        if (null != entity.getAttrList() && entity.getAttrList().size() > 0) {
            List<Attr> attrList = entity.getAttrList();
            for (Attr attr : attrList) {
                re = attrMapper.insert(attr);
                isSuccess(re, "保存attr失败");

                if (null != attr.getAttrValues() && attr.getAttrValues().size() > 0) {
                    List<AttrValue> attrValueList = attr.getAttrValues();
                    for (AttrValue attrValue : attrValueList) {
                        attrValue.setAttr_id(attr.getAttr_id());
                        re = attrValueMapper.insert(attrValue);
                        isSuccess(re, "保存attr值失败");
                    }
                }

                //保存商品属性关系值
                re = spuAttrValueMapper.saveSpuAttrValue(entity.getSpu_id(), attr.getAttr_id());
                isSuccess(re,"保存spu属性值失败");

                //保存spu sku 属性值
                re = spuSkuAttrValueMapper.saveSpuSkuAttrValue(entity.getSpu_id());
                isSuccess(re,"保存spu属性值失败");
            }
        }

        //保存spu扩展信息
        SpuExtension spuExtension = new SpuExtension();
        spuExtension.setSpu_id(entity.getSpu_id());
        spuExtension.setActual_stock(total);
        spuExtension.setStock(total);
        spuExtensionMapper.insert(spuExtension);

        SpuDetail spuDetail = new SpuDetail();
        spuDetail.setSpu_id(entity.getSpu_id());
        spuDetail.setDetail(entity.getDetail());
        re = spuDetailMapper.insert(spuDetail);
        isSuccess(re, "spu详情插入失败");

        return 1;
    }

    @Override
    public PageResult<Map> querySpuByPage(SpuQuery spuQuery) {

        PageHelper.startPage(spuQuery.getPage(),spuQuery.getPageSize());
        List<Map> list = baseMapper.querySpuByPage(spuQuery);
        PageInfo<Map> pageInfo = new PageInfo<>(list);

        return new PageResult<>(spuQuery.getPage(),spuQuery.getPageSize(),pageInfo.getTotal(),list);
    }

    @Override
    public Map querySpuById(Long id) {
        return baseMapper.querySpuById(id);
    }


    /**
     * 分布式事务更新库存
     * @param spuBeanList
     * @return
     */
    @Transactional
    @Override
    public int updateSpuStock(List<SpuBean> spuBeanList) {

        if (null == spuBeanList || spuBeanList.size() == 0)
            return 0;
        for (SpuBean spuBean : spuBeanList) {
            //添加sku锁定表数据
            SkuStockLock skuStockLock = new SkuStockLock();
            skuStockLock.setSku_id(spuBean.getSku_id());
            skuStockLock.setSpu_id(spuBean.getSpu_id());
            skuStockLock.setOrder_id(spuBean.getOrder_id());
            skuStockLock.setCount(spuBean.getCount());

            int re = skuStockLockMapper.insert(skuStockLock);
            isSuccess(re,"添加sku数据失败");

            //更新spu库存
            re = spuExtensionMapper.updateSpuStock(spuBean);
            isSuccess(re,"更新spu库存数据失败");

            //更新sku库存
            re = skuStockMapper.updateSkuStock(spuBean);
            isSuccess(re,"更新sku库存数据失败");

        }
        return 1;
    }
}
